package web;


import com.smart.dao.TeacherDao;
import com.smart.domain.Calender;
import com.smart.domain.Teacher;
import com.smart.service.CalenderServiceImp;
import com.smart.service.TeacherServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


@Controller
public class TeacherController {



   private TeacherServiceImp teacherServiceImp;
   private CalenderServiceImp calenderServiceImp;



    @RequestMapping(value = "/index.html")
    public String loginPage(){
        return "login";
    }

    @RequestMapping(value = "/loginCheck.html")
    public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) {
        Teacher teacher=new Teacher();
        teacher.setUserName(loginCommand.getUserNamecom());
        teacher.setPassword(loginCommand.getPasswordcom());
        boolean isValidteacher = teacherServiceImp.hasMatchTeacher(teacher);
        System.out.println(isValidteacher);
        if (!isValidteacher) {
            return new ModelAndView("login", "error", "用户名或密码错误。");

        } else {
            Teacher teacher1 = teacherServiceImp.queryteacher(loginCommand.getUserNamecom());
            request.getSession().setAttribute("teacher", teacher1);
            return new ModelAndView("AddCalender");
        }
    }
//增加老师信息返回信息页面
    @RequestMapping(value = "/AddTeacher.html")
    public  String a(Teacher teacher, HttpServletRequest request, HttpServletResponse response){
        String n=request.getParameter("teacher_name");
        String d=request.getParameter("teacher_department");
        String u=request.getParameter("teacher_username");
        String p=request.getParameter("teacher_password");
        String e=request.getParameter("email");
        String t=request.getParameter("phonenumber");
        int te=request.getIntHeader("user_type");
        teacher.setName(n);
        teacher.setDepartment(d);
        teacher.setUserName(u);
        teacher.setPassword(p);
        teacher.setEmail(e);
        teacher.setPhoneNumber(t);
        teacher.setUser_tye(te);

        teacherServiceImp.addteacher(teacher);
        return "main";
    }


    @Autowired

    public void setTeacherServiceImp(TeacherServiceImp teacherServiceImp) {
        this.teacherServiceImp = teacherServiceImp;
    }
    @RequestMapping(value="/AddCalender.html")
    public String a(Calender calender, HttpServletRequest request, HttpServletResponse response){
       int t= Integer.parseInt(request.getParameter("user_term"));
        int w= Integer.parseInt(request.getParameter("user_weeks"));
      //  Date st= Date.valueOf(request.getParameter("user_startDay"));
       // Date sp= Date.valueOf(request.getParameter("user_stopDay"));
        //calender.setTerm(4);
        //calender.setWeek(9);
       // calender.setStartday(st);
        //calender.setStopday(sp);
        //calenderServiceImp.addcal(calender);
        return "main";
    }

}

